package com.example.developdemo.controller;

import com.example.developdemo.dto.Result;
import com.example.developdemo.entity.Book;
import com.example.developdemo.service.BookService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class BookController {

    @Resource
    private BookService bookService;

    /**
     * 添加图书
     * @RequestBody 入参采用json格式
     * post请求的参数 在请求体body里面
     */
    @PostMapping(value = "/addBook")
    public Result addBook(@RequestBody Book book){
        bookService.addBook(book);
        return Result.ok();
    }

    /**
     * 删除图书（单个）
     */
    @GetMapping(value = "/deleteBook")
    public Result deleteBook(Integer bookId){
        bookService.deleteBookByBookId(bookId);
        return Result.ok();
    }

    /**
     * 批量删除图书
     */
    @DeleteMapping(value = "/batchDeleteBook")
    public Result batchDeleteBook(@RequestBody List<Integer> bookIds){
        bookService.batchDeleteBook(bookIds);
        return Result.ok();
    }

    /**
     * 更新图书
     */
    @PostMapping(value = "/updateBook")
    public Result updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        return Result.ok();
    }

    /**
     * 分页搜索查询
     */
    @GetMapping(value = "/getBooksPage")
    public Result getBooksPage(@RequestParam(value = "pageNo") Integer pageNo,
                               @RequestParam(value = "pageSize") Integer pageSize,
                               String bookName,
                               Integer categoryId,
                               Integer status,
                               @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date start,
                               @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date end){
        return Result.ok(bookService.getBooksPage(pageNo,pageSize,bookName,categoryId,status,start,end));
    }

}
